#include "clearpathrule.h"

#include "../documents/data/graphicdata.h"
#include "../documents/swpprocessdocument.h"
#include "../editors/items/graphicitemcommon.h"

#include <gdmodelquerymaker.h>

using namespace core;
using namespace swp;

ClearPathRule::ClearPathRule() : IValidatorRule()
{

}

ClearPathRule::~ClearPathRule()
{

}

bool ClearPathRule::isProjectWide() const
{
    return false;
}

bool ClearPathRule::validate(
    const QSharedPointer<IContainerDocument>& document,
    QAction*& reportClickAction, QString& reportText) const
{
    return false;
}

bool ClearPathRule::validate(const QSharedPointer<IDocument>& document,
    QAction*& reportClickAction, QString& reportText) const
{
    bool ret = false;

    GraphicData* data = dynamic_cast<GraphicData* >(document->data());

    Q_ASSERT(data != 0); // GraphicData expected

    GDModelQueryMaker queryMaker(*data);

    const QList<IGraphicEditorItem* > sp = queryMaker.getItems(
        SWP_START_POINT_ITEM);
    const QList<IGraphicEditorItem* > ep = queryMaker.getItems(
        SWP_END_POINT_ITEM);
    if (sp.size() == 1 && ep.size() == 1)
    {
        ret = queryMaker.canFindPath(sp.first(), ep.first());
    }
    else
    {
        ret = false;
    }

    reportText =
        QObject::tr("There is no path between Start point and End point");

    return ret;
}
